import { Injectable } from '@angular/core';
import { ISheBao } from '../commons/shebao';
import { ISalary } from '../commons/salary';
import { SheBaoService } from './shebao.service';

@Injectable()
export class SalaryService {

    constructor(private sheBaoService: SheBaoService) { }

    /**
     * 计算各项社保/总工资/基数扣税工资/实发工资
     * @param jiBen
     * @param jiXiao
     * @param jiangJin
     */
    public doCalc(jiBen: number, jiXiao: number, jiangJin: number): ISalary {
        // 总额
        let total = jiBen + jiXiao + jiangJin;
        // 计税工资（扣除社保后的工资）
        let jiShui = total - jiBen * (
            this.sheBaoService.rate.yangLao +
            this.sheBaoService.rate.yiLiao +
            this.sheBaoService.rate.shiYe +
            this.sheBaoService.rate.gongJiJin
        ) - this.sheBaoService.rate.tongChou;

        // 个税
        let shui = this.calcShui(jiShui);

        let shiFa = jiShui - shui;

        return {
            total: total,
            sheBao: {
                yangLao: jiBen * this.sheBaoService.rate.yangLao,
                yiLiao: jiBen * this.sheBaoService.rate.yiLiao,
                shiYe: jiBen * this.sheBaoService.rate.shiYe,
                gongShang: jiBen * this.sheBaoService.rate.gongShang,
                shengYu: jiBen * this.sheBaoService.rate.shengYu,
                gongJiJin: jiBen * this.sheBaoService.rate.gongJiJin,
                tongChou: this.sheBaoService.rate.tongChou,
            },
            jiShui: jiShui,
            shui: shui,
            shiFa: shiFa
        }
    }

    /**
     * 计算个人所得税
     * 返回计税基数和税额
     */
    private calcShui(jiShui: number): number {
        let base = jiShui > 3500 ? jiShui - 3500 : 0;
        let tax = 0;
        // 计算个人所得税
        if (base <= 0) {
            tax = 0
        } else if (base <= 1500) {
            tax = base * 0.03
        } else if (base <= 4500) {
            tax = base * 0.1 - 105
        } else if (base <= 9000) {
            tax = base * 0.2 - 555
        } else if (base <= 35000) {
            tax = base * 0.25 - 1005
        } else if (base <= 55000) {
            tax = base * 0.3 - 2755
        } else if (base <= 80000) {
            tax = base * 0.35 - 5505
        } else {
            tax = base * 0.45 - 13505
        }

        return tax;
    }
}